package cn.jobstart.service.impl;

import cn.jobstart.common.IdGen;
import cn.jobstart.common.NumberUtil;
import cn.jobstart.common.StringUtil;
import cn.jobstart.dao.IResumeBasicDao;
import cn.jobstart.enums.*;
import cn.jobstart.es.vo.ResumeEsVO;
import cn.jobstart.modelExt.PersonExt;
import cn.jobstart.modelExt.ResumeBasicExt;
import cn.jobstart.pubInter.AbstractBaseService;
import cn.jobstart.pubInter.IBaseDao;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.ResumeBasicSearchExt;
import cn.jobstart.service.IPersonService;
import cn.jobstart.service.IResumeBasicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author sunke
 * @Date 2023-09-06 15:00:21
 * @description ResumeBasicService      简历基础表
 */
@Service(value = "resumeBasicService")
@Slf4j
public class ResumeBasicServiceImpl extends AbstractBaseService<ResumeBasicExt, ResumeBasicSearchExt, String> implements IResumeBasicService {

    @Resource(name = "resumeBasicDao")
    private IResumeBasicDao resumeBasicDao;



    @Resource
    private IPersonService personService;


    /**
     * 得到共得到简历数
     *
     * @param talentSubscriptionTaskId
     * @return
     */
    @Override
    public int getCountByTalentSubscriptionTaskId(String talentSubscriptionTaskId) {
        if(StringUtil.isNullOrEmpty(talentSubscriptionTaskId)){
            return 0;
        }

        return getCountBySearch(ResumeBasicSearchExt.getInstance().setTalentSubscriptionTaskIdEq(talentSubscriptionTaskId));
    }

    @Override
    public IBaseDao<ResumeBasicExt, ResumeBasicSearchExt, String> getDao() {
        return resumeBasicDao;
    }


    @Override
    public ResumeBasicExt getMiniDetailByOldId(String oldId) {
        if (StringUtil.isNullOrEmpty(oldId)) {
            return null;
        }
        return resumeBasicDao.getMiniDetailByOldId(oldId);
    }

    @Override
    public ResumeBasicExt getDetailByOldResumeId(String oldResumeId) {
        if (StringUtil.isNullOrEmpty(oldResumeId)) {
            return null;
        }


        ResumeBasicSearchExt search = ResumeBasicSearchExt.getInstance();
        search.setOldResumesIdEq(oldResumeId);

        return getDetailBySearch(search);
    }

    /**
     * 只查询一个id
     *
     * @param search
     * @return
     */
    @Override
    public List<String> getIdListBySearch(ResumeBasicSearchExt search) {
        try {
            long startTime = System.currentTimeMillis();


            if (StringUtil.isNullOrEmpty(search)) {
                search = ResumeBasicSearchExt.getInstance();
            }
            List<String> resuList = resumeBasicDao.getIdListBySearch(search);

            log.info("mysql getIdListBySearch: " + (System.currentTimeMillis() - startTime) + " ms");
            return resuList;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public void beforeInsert(ResumeBasicExt obj) throws SkException {

        if (StringUtil.isNullOrEmpty(obj.getEducationFlag()) && StringUtil.isNotObjEmpty(obj.getEducation())) {
            obj.setEducationFlag(ResumeBasicEducationFlagEnum.getDescByType(obj.getEducation()));
        }
        if (StringUtil.isNullOrEmpty(obj.getSex()) && StringUtil.isNotObjEmpty(obj.getSex())) {
            obj.setSexFlag(ResumeBasicSexFlagEnum.getDescByType(obj.getSex()));
        }


        String jobYear = obj.getJobYear();
        if (StringUtil.isNotObjEmpty(jobYear) && StringUtil.isNullOrEmpty(obj.getWorkExperienceFlag())) {
            jobYear = StringUtil.replaceAll(jobYear, "年", "");
            obj.setWorkExperienceFlag(AdminJobWorkExperienceFlagEnum.getTypeByYear(NumberUtil.parseInt(jobYear)));
        }

        if(StringUtil.isNullOrEmpty(obj.getPoliticsFlag())&& StringUtil.isNotObjEmpty(obj.getPolitics())){
            obj.setPoliticsFlag(ResumeBasicPoliticsFlagEnum.getDescByType(obj.getPolitics()));
        }

        if(StringUtil.isNullOrEmpty(obj.getMarriageFlag())&& StringUtil.isNotObjEmpty(obj.getMarriage())){
            obj.setMarriageFlag(ResumeBasicMarriageFlagEnum.getDescByType(obj.getMarriage()));
        }



        String personId = obj.getPersonId();
        if (StringUtil.isNullOrEmpty(personId)) {
            PersonExt person = new PersonExt();
            person.setId(IdGen.uuid());
            person.setTruename(obj.getTruename());
            person.setPhone(obj.getMobile());
            person.setIdcard(obj.getIdcard());
            personId = person.getId();
            personService.insert(person);
        }
        obj.setPersonId(personId);

        super.beforeInsert(obj);
    }

    /**
     * 根据人ID 找到简历
     *
     * @param personId
     * @return
     */
    @Override
    public List<ResumeBasicExt> getListByPersonId(String personId) {
        try {
            if (StringUtil.isNullOrEmpty(personId)) {
                return null;
            }
            ResumeBasicSearchExt search = ResumeBasicSearchExt.getInstance();
            search.setPersonIdEq(personId);
            return getAllList(search);
        } catch (SkException e) {
            return null;
        }
    }
}